//
//  WmcClassifyAlbumSecondLevelVC.swift
//  YPWatermarkCamera
//
//  Created by 周诗松 on 2022/5/14.
//

import UIKit
import MJRefresh

// MARK: - 扩展WmcClassifyAlbumSecondLevelVC + WmcNavigatorProtocol

extension WmcClassifyAlbumSecondLevelVC: WmcNavigatorProtocol {
    static func navigationPaths() -> [WmcNavigatorManager.Path] {
        return [.classifyAlbumSecondLevel]
    }

    static func willMakeNavigationVC(with parms: WmcNavigatorManager.Parms) -> UIViewController {
        var vm = WmcClassifyAlbumSecondLevelVM()

        if let viewModel = parms.context as? WmcClassifyAlbumSecondLevelVM {
            vm = viewModel
        }

        let vc = WmcClassifyAlbumSecondLevelVC(vm)
        return vc
    }
}

// MARK: - 扩展WmcClassifyAlbumSecondLevelVC + WmcVMProtocol

extension WmcClassifyAlbumSecondLevelVC: WmcVMProtocol {
    typealias VMType = WmcClassifyAlbumSecondLevelVM
}

// MARK: - [WmcClassifyAlbumSecondLevelVC]

class WmcClassifyAlbumSecondLevelVC: WmcBaseVC {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        title = viewModel.title
        refresh()
    }
    
    private func refresh() {
        WmcHudManager.loading()
        viewModel.fetchList { [weak self] list in
            guard let weakSelf = self else { return }
            WmcHudManager.dismiss()
            weakSelf.tableView.reloadData()
            weakSelf.tableView.mj_header?.endRefreshing()
            
            weakSelf.emptyView.isHidden = !list.isEmpty
            weakSelf.showRequestFailure(isShow: false)
        } failure: { [weak self] error in
            guard let weakSelf = self else { return }
            WmcHudManager.dismiss()
            WmcHudManager.toast(error)
            weakSelf.showRequestFailure(isShow: true)
        }
    }
    
    private func showRequestFailure(isShow: Bool) {
        if viewModel.dataSource.isEmpty {
            tableView.isHidden = isShow
            badNetworkView.isHidden = !isShow
        } else {
            badNetworkView.isHidden = true
            tableView.isHidden = false
        }
    }
    
    override func makeUI() {
        makeNavView(showShadow: false)
        
        addSubview(tableView)
        addSubview(badNetworkView)
        
        tableView.addSubview(emptyView)
        
        tableView.snp.makeConstraints { make in
            make.top.equalTo(navView.snp.bottom)
            make.left.right.bottom.equalToSuperview()
        }
        
        emptyView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        badNetworkView.snp.makeConstraints { make in
            make.edges.equalTo(tableView)
        }
    }
    
    override func bindViewAction() {
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.refresh()
        })
        
        badNetworkView.refreshClosure = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.refresh()
        }
    }
    
    private lazy var tableView: UITableView = {
        let view = UITableView(frame: CGRect.zero, style: .plain)
        view.backgroundColor = .wmc_white_FFFFFF
        view.delegate = self
        view.dataSource = self
        view.register(WmcClassifyAlbumSecondLevelCell.self, forCellReuseIdentifier: "WmcClassifyAlbumSecondLevelCell")
        view.separatorStyle = .none
        return view
    }()
    
    private lazy var badNetworkView: WmcBadNetworkEmptyView = {
        let view = WmcBadNetworkEmptyView()
        view.isHidden = true
        return view
    }()
    
    private lazy var emptyView: WmcCloudAllPhotoEmptyView = {
        let view = WmcCloudAllPhotoEmptyView()
        view.isHidden = true
        return view
    }()
}

// MARK: - UITableViewDelegate, UITableViewDataSource

extension WmcClassifyAlbumSecondLevelVC: UITableViewDelegate, UITableViewDataSource {
    func numberOfSections(in _: UITableView) -> Int {
        return 1
    }

    func tableView(_: UITableView, numberOfRowsInSection _: Int) -> Int {
        return viewModel.dataSource.count
    }

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell: WmcClassifyAlbumSecondLevelCell = tableView.dequeueReusableCell(withIdentifier: "WmcClassifyAlbumSecondLevelCell", for: indexPath) as! WmcClassifyAlbumSecondLevelCell
        if let model = viewModel.dataSource.yp.object(at: indexPath.row) {
            cell.bindModel(model)
        }
        return cell
    }

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard let model = viewModel.dataSource.yp.object(at: indexPath.row) else { return }
        
        let vm = WmcClassifyAlbumDetailVM()
        vm.title = model.name
        vm.type = .watermarkPhoto
        vm.watermarkId = model.wmId
        vm.dataNeedUpdate = { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.viewModel.dataNeedUpdate?()
            weakSelf.refresh()
        }
        WmcNavigatorManager.push(.allPhoto, parms: vm)?.hidesBottomBarWhenPushed = true
    }

    func tableView(_: UITableView, heightForRowAt _: IndexPath) -> CGFloat {
        return 88
    }
}
